<?php
/*
	找人代付
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'main';
$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['orderid']);
	if(!$orderid){
		exi('请先选择订单！','error');
	}
	$setting=$this->plugin_setting('peerpay');
	if(!$setting['open']){
		exi('系统暂不支持找人代付！','error');
	}
	$check=$this->pdo_getcolumn('order_peerpay',array('orderid'=>$orderid),'id');
	if($check){
		exi('','','//share/orderid='.$orderid);
	}
	$return['order']=$this->pdo_get('order',array('id'=>$orderid),array('price'));
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar'));
    $ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),array('goodsid','istaocan','optionid','price','total','goodssn','productsn'));
    foreach($ordergoods as &$gv) {
        $goodsinfo = pdo_get('jy_weishop_goods', array('id' => $gv['goodsid'], 'uniacid' => $_SESSION['uniacid']), array('id', 'marketprice', 'unit', 'image', 'title', 'merchid'));
        $gv['title'] = $goodsinfo['title'];
        $gv['image'] = tomedia($goodsinfo['image']);
        $gv['weight'] = pdo_getcolumn('jy_weishop_goods_dispatch', array('goodsid' => $gv['goodsid']), 'weight');
        //商品规格
        if($gv['optionid']){
            $options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
            if($gv['istaocan']){
                $gv['selespename']=$options['title'];
            }else{
                $specs=$options['specs'];
                $specs=explode("_",$specs);
                foreach($specs as &$spec){
                    $spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
                }
                $gv['selespename']=implode("，",$specs);
            }
        }
    }
    $return['ordergoods']=$ordergoods;
    $return['goods']=$this->pdo_get('goods',array('id'=>$ordergoods[0]['goodsid']),array('id','title','image'));
	$return['goods']['image']=$return['goods']['image']?tomedia($return['goods']['image']):'';
	$item=array(
		'type'=>1,
		'orderid'=>$orderid
	);
	if($setting['remarks']){
		$item['remark']=$setting['remarks'][array_rand($setting['remarks'],1)]['remark'];
	}
	set_html('title','找人代付');
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	if(!$orderid){
		exi('请先选择订单！','error');
	}
	$peerpay=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'orderid'=>$orderid,
		'type'=>intval($post['type']),
		'money'=>$this->pdo_getcolumn('order',array('id'=>$orderid),'price'),
		'createtime'=>TIMESTAMP,
		'remark'=>trim($post['remark']),
		'status'=>0,
	);
	
	$this->pdo_insert('order_peerpay',$peerpay);
	$this->pdo_update('order',array('paytype'=>'peerpay'),array('id'=>$orderid));
	exi('','','//share/orderid='.$peerpay['orderid']);
}
if($_RQ['op']=='share'){
	$orderid=intval($_RQ['orderid']);
	if(!$orderid){
		exi('请先选择订单！','error');
	}
	$order=$this->pdo_get('order',array('id'=>$orderid));
	$return['member']=model('core/member')->detail($order['mid'],array('nickname','avatar'));
	$peerpay=$this->pdo_get('order_peerpay',array('orderid'=>$orderid));
	if(!$peerpay){
		exi('指定订单未设置代付或订单不存在！','error');
	}
	$pay_money=$this->pdo_getsum('order_peerpay_log',array('orderid'=>$orderid,'status'=>1),'money');
	$rate=round($pay_money*100/$peerpay['money'],2);
	$return['rate']=$rate;
	$return['need_money']=$peerpay['money']-$pay_money;
	$return['orderid']=$orderid;
	$goods_id=$this->pdo_getcolumn('order_goods',array('orderid'=>$orderid),'goodsid');
	$goods_image=$this->pdo_getcolumn('goods',array('id'=>$goods_id),'image');
	if($order['mid']!=$_SESSION['uid']){
		$return['peerpay_pay']=1;
	}
	$return['sharedata']=array(
		'title'=>'我想对你说：'.$peerpay['remark'],
		'desc'=>$peerpay['remark'],
		'link'=>custom_url('jy_weishop/order.peerpay_pay//orderid='.$peerpay['orderid'],true),
		'imgUrl'=>tomedia($goods_image),
	);
	exi($return);
}

